/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.impl;

import hibernate.intf.DAO;
import java.util.Collection;
import java.util.List;
import modelmapping.Group;
import modelmapping.VO;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.DataAccessLayerException;
import util.HibernateFactory;


/**
 *
 * @author Valentina
 */
public class GroupDAOImpl implements  DAO{

    private Session session;
    private Transaction tx;
    
    private final String HQL_FIND_ALL = "FROM Group";
    private final String HQL_FIND_BY_NAME = "SELECT * FROM `pentalab`.group WHERE NAME= :code";
    
    public GroupDAOImpl() {
        HibernateFactory.buildIfNeeded();
    }

    private void handleException(HibernateException e) throws DataAccessLayerException {
        HibernateFactory.rollback(tx);
        throw new DataAccessLayerException(e);
    }

    private void startOperation() throws HibernateException {
        session = HibernateFactory.openSession();
        tx = session.beginTransaction();
    }
    
    @Override
    public Long create(VO valueObject) {
        Long id = 0L;

        try {
            startOperation();
            id = (Long) session.save(valueObject);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return id;
    }

    @Override
    public Boolean delete(Long id) throws DataAccessLayerException{
        try {
            startOperation();
            
            Object group = session.load(Group.class, id);
            session.delete(group);
            tx.commit();
            return true;
        } catch (HibernateException e) {
            System.out.println("Delete : "+ e.getMessage());
            handleException(e);
            return false;
        } finally {
            HibernateFactory.close(session);
        }
    }

    @Override
    public Boolean update(VO valueObject) {
        try {
            startOperation();
            session.update(valueObject);
            tx.commit();
            return true;
        } catch (HibernateException e) {
            handleException(e);
            return false;
        } finally {
            HibernateFactory.close(session);
        }
    }

    @Override
    public VO retrieve(Long id) {
        Group valueObject = null;
       
        try {
            startOperation();
            valueObject = (Group) session.get(Group.class, id);
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }finally {
            HibernateFactory.close(session);
        }
        
        return valueObject;
    }

    @Override
    public Collection<? extends VO> retrieve(int pageNumber, int pageSize) {
        
        List<Group> groupList = null;

        try {
            startOperation();
            Query query = session.createQuery(HQL_FIND_ALL);
            
            query = query.setFirstResult(pageSize * (pageNumber - 1));
            query.setMaxResults(pageSize);
            
            groupList = (List<Group>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }finally {
            HibernateFactory.close(session);
        }
        
        return groupList;
    }

    @Override
    public Collection<? extends VO> retrieve() {
                List<Group> groupList = null;

        try {
            startOperation();
            Query query = session.createQuery(HQL_FIND_ALL);

            groupList = (List<Group>) query.list();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        }finally {
            HibernateFactory.close(session);
        }
        return groupList;
    }

    public boolean verifyIfUnicField(String value) {
        List<VO> groupList = null;
        int rowCount =0;
        
        try {
            startOperation();
            Query query = session.createSQLQuery(HQL_FIND_BY_NAME).setParameter("code", value);
            
            System.out.println(query);
            groupList = (List<VO>) query.list();
            tx.commit();
            
            rowCount = groupList.size();
        } catch (HibernateException e) {
            handleException(e);
        }finally {
            HibernateFactory.close(session);
        }
        
        if(rowCount>0){
            return false;
        }else{
            return true;
        }
    }
}